﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FCModel;
using FCDAL;

namespace FCBLL
{
    public class SmartScopeFormatBLL : IFormatBLL
    {
        public IList<ParameterDao> AnalysisFile(string[] arrSourceFile, string fileType)
        {
            IList<ParameterDao> listSSD = new List<ParameterDao>();
            string HeadFlag = arrSourceFile[0].Substring(0, 6);
            ParameterDao ssd = new ParameterDao();
            //标志是否有连续多行多个参数计数
            int iContinueRow = 0;
            int iSeqNo = 1;
            foreach (string rowData in arrSourceFile)
            {
                string[] tmpArrData = rowData.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                if (rowData.Contains(HeadFlag))
                {                   
                    ssd = new ParameterDao();
                    ssd.SeqNo = iSeqNo.ToString();
                    iContinueRow = 0;
                    continue;
                }
                if (tmpArrData.Length == 0) continue;
                if (tmpArrData[0].Equals("EOT", StringComparison.OrdinalIgnoreCase))
                {
                    if (ssd.listSSD.Count > 0)
                        listSSD.Add(ssd);
                    iContinueRow = 0;
                    iSeqNo++;
                }
                if (tmpArrData[0].Equals("EOM", StringComparison.OrdinalIgnoreCase))
                {
                    iContinueRow = 0;
                }
                else
                    iContinueRow++;
                if (iContinueRow == 0 || tmpArrData.Length < 2)
                    continue;//过滤EOM/EOT行
                string[] paramName = tmpArrData[tmpArrData.Length - 1].Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                Parameter ss = new Parameter();
                if (paramName.Length > 1)//多个参数在同一行
                {
                    ss.ParameterName = paramName[iContinueRow - 1];
                }
                else
                {
                    ss.ParameterName = tmpArrData[tmpArrData.Length - 1];
                }
                try
                {
                    string[] tmparrsortname = ss.ParameterName.Split('-');
                    ss.ParameterSortName = tmparrsortname[0].Length == 4 ? tmparrsortname[0].Substring(0, 3) + "0" + tmparrsortname[0].Substring(3, 1) : tmparrsortname[0];
                    if (tmparrsortname.Length > 1)
                        ss.ParameterSortName += "-" + (tmparrsortname[1].Length == 2 ? tmparrsortname[1] : "0" + tmparrsortname[1]);
                }
                catch { }
                ss.ParameterValue = Math.Abs(float.Parse(tmpArrData[1])); //Abs目前是负号只是坐标的方向，并不代表值的大小
                //通过基础数据获取标准值
                //ss.StandardValue = float.Parse(tmpArrData[2]);
                ssd.listSSD.Add(ss);
            }
            return listSSD;
        }

    }
}
